// 邮箱脱敏
export function encryptEmail (email) {
  if (email) {
    let new_email = email;
    if (String(email).indexOf('@') > 0) {
      let str = email.split('@');
      let _s = '';
      if (str[0].length > 3) { //@前面多于3位
        for (let i = 3; i < str[0].length; i++) {
          _s += '*';
        }
        new_email = str[0].substr(0, 3) + _s + '@' + str[1];
      } else { //@前面小于等于于3位
        for (let i = 1; i < str[0].length; i++) {
          _s += '*'
        }
        new_email = str[0].substr(0, 1) + _s + '@' + str[1]
      }
    }
    return new_email;
  }
  return ''
}

// 身份证脱敏
export function encryptIdCard (val) {
  if (val) {
    if (Number(val.length) < 10) {
      var reg = /(\d{1})\d*(\d{1})/;
      return val.replace(reg, "$1**********$2")
    } else {
      var reg = /(\d{4})\d*(\d{4})/;
      return val.replace(reg, "$1**********$2")
    }
  }
  return ''
}

// 手机号脱敏
export function encryptPhone (val) {
  if (val) {
    var reg = /(\d{3})\d{4}(\d{4})/;
    return val.replace(reg, "$1****$2")
  }
  return ''
}

// 名称脱敏
export function encryptName (str) {
  if (str) {
    if (str.length <= 3) {
      return "*" + str.substring(1, str.length);
    } else if (str.length > 3 && str.length <= 6) {
      return "**" + str.substring(2, str.length);
    } else if (str.length > 6) {
      return str.substring(0, 2) + "****" + str.substring(6, str.length)
    }
  } else {
    return "";
  }
}

export function getSpecifiedTime (dateTime) {
  if (dateTime) {
    // 指定日期和时间
    var EndTime = new Date(dateTime);
    // 当前系统时间
    var NowTime = new Date();
    var t = EndTime.getTime() - NowTime.getTime();
    var d = Math.floor(t / 1000 / 60 / 60 / 24);
    var h = Math.floor(t / 1000 / 60 / 60 % 24);
    var m = Math.floor(t / 1000 / 60 % 60);
    var s = Math.floor(t / 1000 % 60);
    var date = d;
    return date;
  } else {
    return ''
  }
}
